using Digitalmes.Application.Sys.Roles;
using Digitalmes.Application.Sys.Roles.Commands;
using Digitalmes.Application.Sys.Roles.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 角色终结点。
/// </summary>
public sealed class RoleEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "role")
           .RequireAuthorization()
           .MapGet(GetRoleListAsync, "list")
           .MapGet(GetRolePagedListAsync, "list-page")
           .MapGet(GetRoleByIdAsync, "{id}")
           .MapPost(CreateRoleAsync)
           .MapPut(UpdateRoleAsync, "{id}")
           .MapDelete(DeleteRoleAsync, "{id}")
           .MapPost(GrantMenusAsync, "grant-menus");
    }

    [EndpointDescription("获取系统角色集合")]
    public async Task<IApiResult> GetRoleListAsync(ISender sender, [AsParameters] GetRoleListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页获取系统角色集合")]
    public async Task<IApiResult> GetRolePagedListAsync(ISender sender, [AsParameters] GetRoleListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("按 Id 获取系统角色")]
    public async Task<IApiResult> GetRoleByIdAsync(ISender sender, long id)
    {
        var result = await sender.Send(new GetRoleByIdQuery(id));
        return result.ToApiResult();
    }

    [EndpointDescription("创建系统角色")]
    public async Task<IApiResult> CreateRoleAsync(ISender sender, SysRoleDto dto)
    {
        var result = await sender.Send(new CreateRoleCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新系统角色")]
    public async Task<IApiResult> UpdateRoleAsync(ISender sender, long id, SysRoleDto dto)
    {
        var result = await sender.Send(new UpdateRoleCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除系统角色")]
    public async Task<IApiResult> DeleteRoleAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteRoleCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("角色授予菜单")]
    public async Task<IApiResult> GrantMenusAsync(ISender sender, GrantMenusCommand command)
    {
        var result = await sender.Send(command);
        return result.ToApiResult();
    }
}
